package com.justgame.project.service.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.justgame.project.cloud.common.constant.Result;
import com.justgame.project.cloud.common.util.MapBuilder;
import com.justgame.project.service.system.entity.SysStationOrder;
import com.justgame.project.service.system.dto.SysStationOrderDTO;
import com.justgame.project.service.system.wrapper.SysStationOrderWrapper;
import com.justgame.project.service.system.controller.form.SysStationOrderQueryForm;
import com.justgame.project.service.system.service.ISysStationOrderService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.Assert;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 *
 * 平台站内订单控制器
 *
 * @@tableName sys_station_order
 * @author Zwb
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/sys/station/order")
@Api("平台站内订单控制器")
public class SysStationOrderController {

    private final ISysStationOrderService sysStationOrderService;

    @GetMapping("/detail")
    @ApiOperation(value = "通过主键id查询 平台站内订单")
    public Result<?> query(@ApiParam("主键id") @NotNull(message = "必须提供需要查询的数据") Long id){
        return Result.ok(sysStationOrderService.getById(id));
    }

    @PutMapping("/update")
    @RequiresAuthentication
    @ApiOperation(value = "通过主键id编辑 平台站内订单")
    public Result<?> edit(@ApiParam("编辑表单") @RequestBody @Validated SysStationOrderDTO dto){
        Assert.notNull(dto.getId(),"必须提供id");
        Assert.notNull(dto.getGameInfoId(),"必须提供游戏信息");
        Assert.notNull(dto.getCostCoins(),"必须提供购买消耗金币");
        Assert.notNull(dto.getBeforePaidCoins(),"必须提供支付前拥有金币");
        Assert.notNull(dto.getExpireDate(),"必须提供订单支付过期时间");
        Assert.notNull(dto.getOrderNo(),"必须提供订单编号");
        Assert.notNull(dto.getCostIntegral(),"必须提供购买消耗积分");
        Assert.notNull(dto.getBeforePaidIntegral(),"必须提供支付前拥有积分");
        Assert.notNull(dto.getRemark(),"必须提供备注");
        SysStationOrder sysStationOrder = sysStationOrderService.requireGetById(dto.getId(),"相关数据不存在");
        sysStationOrder.setGameInfoId(dto.getGameInfoId());
        sysStationOrder.setCostCoins(dto.getCostCoins());
        sysStationOrder.setBeforePaidCoins(dto.getBeforePaidCoins());
        sysStationOrder.setExpireDate(dto.getExpireDate());
        sysStationOrder.setOrderNo(dto.getOrderNo());
        sysStationOrder.setCostIntegral(dto.getCostIntegral());
        sysStationOrder.setBeforePaidIntegral(dto.getBeforePaidIntegral());
        sysStationOrder.setRemark(dto.getRemark());
        return Result.status(sysStationOrderService.updateById(sysStationOrder));
    }

    @PostMapping("/save")
    @RequiresAuthentication
    @ApiOperation(value = "新增 平台站内订单")
    public Result<?> save(@ApiParam("新增表单") @RequestBody @Validated SysStationOrderDTO dto){
        Assert.notNull(dto.getGameInfoId(),"必须提供游戏信息");
        Assert.notNull(dto.getCostCoins(),"必须提供购买消耗金币");
        Assert.notNull(dto.getBeforePaidCoins(),"必须提供支付前拥有金币");
        Assert.notNull(dto.getExpireDate(),"必须提供订单支付过期时间");
        Assert.notNull(dto.getOrderNo(),"必须提供订单编号");
        Assert.notNull(dto.getCostIntegral(),"必须提供购买消耗积分");
        Assert.notNull(dto.getBeforePaidIntegral(),"必须提供支付前拥有积分");
        Assert.notNull(dto.getRemark(),"必须提供备注");
        SysStationOrder sysStationOrder = new SysStationOrder();
        sysStationOrder.setGameInfoId(dto.getGameInfoId());
        sysStationOrder.setCostCoins(dto.getCostCoins());
        sysStationOrder.setBeforePaidCoins(dto.getBeforePaidCoins());
        sysStationOrder.setExpireDate(dto.getExpireDate());
        sysStationOrder.setOrderNo(dto.getOrderNo());
        sysStationOrder.setCostIntegral(dto.getCostIntegral());
        sysStationOrder.setBeforePaidIntegral(dto.getBeforePaidIntegral());
        sysStationOrder.setRemark(dto.getRemark());
        return Result.status(sysStationOrderService.save(sysStationOrder));
    }

    @DeleteMapping("/remove")
    @RequiresAuthentication
    @ApiOperation(value = "通过主键id删除 平台站内订单")
    public Result<?> remove(@ApiParam("主键id") @NotNull(message = "必须提供需要删除的数据") Long id){
        return Result.status(sysStationOrderService.requireRemoveById(id, "抱歉，您的账号权限不支持删除此数据"));
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public Result<?> page(@ApiParam("查询表单") SysStationOrderQueryForm queryForm){
        IPage<SysStationOrderWrapper> page = sysStationOrderService.selectJoinListPage(queryForm.buildPageQuery());
        return Result.ok(MapBuilder.builder()
                    .put("data",page.getRecords())
                    .put("rows",page.getTotal())
                .build());
    }

    @GetMapping("/list")
    @ApiOperation(value = "列表查询")
    public Result<?> list(@ApiParam("查询表单") SysStationOrderQueryForm queryForm){
        List<SysStationOrderWrapper> list = sysStationOrderService.selectJoinList(queryForm.buildListQuery());
        return Result.ok(MapBuilder.builder()
                    .put("data",list)
                .build());
    }
}